<?php
/**
 * 统计
 *
 * @author zhoujf <camfee@foxmail.com>
 */

use Common\Bridge;

define('SITE_AUTH', 'statistics-pay');
require('../../../common.inc.php');

class pay extends \lib\core\AdminAction
{
    public function doDefault()
    {
        //计算时间
        $today_date = date('Y-m-d');
        $today = strtotime($today_date);
        $seven_time = $today - 604800;
        $seven_day = date('Y-m-d', $seven_time);
        $yesterday = date('Y-m-d', $today - 86400);
        $type_pay = loadconf('statistics/type')['pay'];

        //查询数据
        $pdo = Bridge::pdo(Bridge::DB_ADMIN_R);
        $sql = 'SELECT `Type`,`Num`,`CreateDate` FROM `SiteStatus` WHERE `Type` = "' . $type_pay . '" AND `CreateDate` >= "' . $seven_day . '" AND `CreateDate` <= "' . $yesterday . '" ORDER BY `CreateDate` ASC';
        $obj = $pdo->query($sql);
        $list = $obj->fetchAll();
        $list_data = [];
        foreach ($list as $k => $v) {
            $list_data[$v['CreateDate']][$v['Type']] = $v['Num'];
        }
        $data = [];
        for ($i = $seven_time; $i < $today; $i = $i + 86400) {
            $data['date'][] = date('Y-m-d', $i);
        }
        foreach ($data['date'] as $k => $v) {
            $data['type_pay'][] = $list_data[$v][$type_pay] ? (int)$list_data[$v][$type_pay] : 0;
        }
        $data['str_day'] = $seven_day;
        $data['end_day'] = $yesterday;

        $smarty = $this->app->page();
        $smarty->value('data', $data);
        $smarty->output('statistics/pay.tpl');
    }

    public function doAjaxSelect()
    {
        $str_date = trim($_GET['str_date']);
        $end_date = trim($_GET['end_date']);

        $str_time = strtotime($str_date);
        $end_time = strtotime($end_date);
        $type_pay = loadconf('statistics/type')['pay'];

        $pdo = Bridge::pdo(Bridge::DB_ADMIN_R);
        $sql = 'SELECT `Type`,`Num`,`CreateDate` FROM `SiteStatus` WHERE `Type` = "' . $type_pay . '" AND `CreateDate` >= "' . $str_date . '" AND `CreateDate` <= "' . $end_date . '" ORDER BY `CreateDate` ASC';
        $obj = $pdo->query($sql);
        $list = $obj->fetchAll();
        $list_data = [];
        foreach ($list as $k => $v) {
            $list_data[$v['CreateDate']][$v['Type']] = $v['Num'];
        }
        $data = [];
        for ($i = $str_time; $i < $end_time + 86400; $i = $i + 86400) {
            $data['date'][] = date('Y-m-d', $i);
        }
        foreach ($data['date'] as $k => $v) {
            $data['type_pay'][] = $list_data[$v][$type_pay] ? (int)$list_data[$v][$type_pay] : 0;
        }

        $this->output(['result' => 'success', 'data' => $data]);
    }
}

$app->run();